<template>
  <div class="app-container" v-if="!openPreview">
    <el-button class="close-btn" icon="el-icon-close" @click="handleClose" />
    <div class="template-header">
      <!-- 首页图标 -->
      <div class="home-icon">
        <img src="@/assets/images/home.png" class="el-icon-house" @click="handleHome" />
      </div>

      <img style="padding-left: 15px" src="@/assets/images/templateC.png" />
      <div class="header-title">
        <div class="main-title">
          <span class="title-text">模版中心</span>
        </div>
        <div class="sub-title">专业模板，让每个文档都成为佳作</div>
      </div>
    </div>
    <hr style="color: darkgray" />
    <br />
    <!-- 搜索和操作栏 -->
    <el-row :gutter="20" class="mb8">
      <el-col :span="18">
        <el-input v-model="queryParams.keyword" placeholder="请输入模板名称" clearable @keyup.enter.native="handleQuery"
          style="width: 300px" />
        <el-button type="primary" @click="handleQuery">搜索</el-button>
      </el-col>
      <el-col :span="6" class="text-right">
        <el-button type="primary" @click="handleImport"> 增加模板 </el-button>
      </el-col>
    </el-row>

    <!-- 模板表格 -->
    <el-table :data="templateList" style="width: 100%" :fit="true" :header-cell-style="{ background: '#f5f7fa' }">
      <el-table-column prop="name" label="模板名称" width="280" />
      <el-table-column prop="usageCount" label="使用量" width="130" align="center" />
      <el-table-column prop="publishTime" label="发布时间" width="330" align="center">
        <template slot-scope="scope">
          <span>
            {{
              scope.row.publishTime ? parseTime(scope.row.publishTime) : "--"
            }}</span>
        </template>
      </el-table-column>

      <el-table-column prop="fileType" label="文件类型" width="300" align="center">
        <template slot-scope="scope">
          <el-tag effect="plain">{{ scope.row.fileType }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="320" align="center">
        <template slot-scope="scope">
          <el-button size="mini" @click="handlePreview(scope.row)">预览</el-button>
          <el-button type="primary" size="mini" @click="handleUse(scope.row)">使用</el-button>
          <el-button type="info" size="mini" icon="el-icon-star-off" @click="handleCollect(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
  </div>
  <div v-else>
    <span size="100" style="height: 900px">
      <!-- 数据库文件编辑显示 -->
      <onlyOffice :previewFile="previewFile" @onlyOfficeClose="onlyOfficeClose" />
    </span>
  </div>
</template>

<script>
import { listTemplate } from "@/api/zhishiku/templates";
import { parseTime } from "@/utils";
import { EventBus } from "@/main";
import onlyOffice from "./onlyOfficeComponents/onlyOffice.vue";

export default {
  name: "templateCenter",

  components:{
    onlyOffice
  },
  data() {
    return {
      openPreview: false, //打开预览
      previewFile: {},    //预览文件
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        keyword: undefined,
        category: "recommend",
      },
      total: 0,
      templateList: [],
      currentTemplate: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleFileOpens(file) {
      console.log("打开文件");
      if (!file?.filePath) {
        this.$message.error("文件路径缺失");
        return;
      }
      this.selectedFile = file;
      this.showEditor = true; // 必须触发状态变更
    },
    getList() {
      listTemplate(this.queryParams).then((response) => {
        this.templateList = [...response.rows];
        console.log("response", response.rows);
        this.total = response.total;
      });
    },
    handleClose() {
      this.$emit("close"); // 触发关闭事件
    },
    handleHome() {
      this.$emit("close"); // 触发关闭事件
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    handlePreview(file) {
      this.previewFile = file;
      this.openPreview = true;
    },
    // 关闭预览
    onlyOfficeClose(){
      this.openPreview =false;
    },
    // 使用
    handleUse(row) {
      console.log("发送模板数据:", row); // 检查数据是否包含必要字段

      // 发送完整数据
      const templateData = { ...row };
      console.log("发送模板数据11:", templateData);
      // 通过EventBus发送模板数据
      EventBus.$emit("open-template-editor", rotemplateData);
      // 关闭模板中心
      this.handleClose();
    },

    handleCollect(row) {
      // 调用收藏接口
    },

    handleImport() {
      // 导入逻辑
    },
  },
};
</script>

<style lang="scss" scoped>
.app-container {
  padding: 20px;
  background: #fff;
}

.preview-content {
  max-height: 70vh;
  overflow-y: auto;
}

/* 新增样式 */
.template-header {
  display: flex;
  align-items: center;

  padding-left: 16px;
  margin-bottom: 24px;

  .home-icon {
    padding-right: 15px;
    border-right: 2px solid #595959;
  }

  .header-title {
    margin-left: 20px;

    .main-title {
      display: flex;
      align-items: baseline;
      margin-bottom: 8px;

      .title-text {
        font-size: 18px;
        font-weight: 500;
        color: #333;
        margin-right: 16px;
      }

      .user-name {
        font-size: 14px;
        color: #666;
      }
    }

    .sub-title {
      font-size: 14px;
      color: #999;
    }
  }
}

/* 关闭按钮样式 */
.close-btn {
  position: absolute;
  right: 20px;
  top: 20px;
  z-index: 999;
}
</style>
